<?php


namespace app\admin\model;


use app\admin\common\Lunar;
use think\Model;
use think\model\concern\SoftDelete;

class Admin extends Model
{
    use SoftDelete;
    protected $name = 'admins';
    protected $hidden = ['password', 'update_time'];


    public function AuthGroupAccess()
    {
        return $this->hasMany(AuthGroupAccess::class, 'uid');
    }

    public function AuthGroups()
    {
        return $this->hasOne(AuthGroup::class, 'id', 'dept_id');
    }

    public function AuthGroup()
    {
        return $this->hasOne(AuthGroup::class, 'id');
    }

    public function dept()
    {
        return $this->hasOne(Department::class, 'id', 'dept_id');
    }

    //生日计算
    public static function Birthday($birthday, $lunar_status)
    {
        //获取当前年份并且转换时间戳做对比使用
        $now_date = strtotime(date('Y-m-d'));
        $lunar = new Lunar();
        if (strpos($birthday, '/') !== false) {
            $avator = str_replace('/', '-', $birthday);
        } else {
            $avator = $birthday;
        }
        if ($lunar_status == 1) {
            $day = explode('-', $avator);
            $brithday = $lunar->convertLunarToSolar(date('Y'), $day[0], $day[1]);
        } else {
            $brithday = date('Y') . '-' . $avator;
        }
        $cha = ((strtotime($brithday) - $now_date) / 86400);
        if ($cha == 0) {
            return "今天生辰";
        } else if ($cha < 0) {
            return "生日祝福已发送";
        } else {
            return "距离生日还剩:" . $cha . '天';
        }

    }

    public function Archives()
    {
        return $this->hasOne(Archives::class, 'admin_id');
    }

}